package com.hao.myfloatwindow;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AppListHelper {
    
    /**
     * 获取应用列表，参考提供的代码逻辑
     * @param context 上下文
     * @return 应用列表
     */
    public static List<AppListBean> getAppListBean(Context context) {
        List<AppListBean> appListBeans = new ArrayList<>();
        PackageManager packageManager = context.getPackageManager();
        List<ApplicationInfo> installedApplications = packageManager.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
        
        for (ApplicationInfo applicationInfo : installedApplications) {
            try {
                PackageInfo packageInfo = packageManager.getPackageInfo(applicationInfo.packageName, PackageManager.GET_ACTIVITIES);
                // 获取app名称
                String name = packageInfo.applicationInfo.loadLabel(packageManager).toString();
                
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    appListBeans.add(new AppListBean(name,
                            // 判断是否系统应用
                            String.valueOf((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 ? 1 : 0), 
                            packageInfo.versionName,
                            String.valueOf(packageInfo.firstInstallTime),
                            (int) (new Date().getTime() / 1000), 
                            packageInfo.packageName,
                            String.valueOf(packageInfo.lastUpdateTime), 
                            String.valueOf(packageInfo.getLongVersionCode())));
                } else {
                    appListBeans.add(new AppListBean(name,
                            // 判断是否系统应用
                            String.valueOf((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 ? 1 : 0), 
                            packageInfo.versionName,
                            String.valueOf(packageInfo.firstInstallTime),
                            (int) (new Date().getTime() / 1000), 
                            packageInfo.packageName,
                            String.valueOf(packageInfo.lastUpdateTime), 
                            String.valueOf(packageInfo.versionCode)));
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
        return appListBeans;
    }
    
    /**
     * 获取所有已安装的用户应用列表（包含图标等信息）
     * 这个方法结合了项目中已有的逻辑，参考getAllInstalledUserApps等方法
     * @param context 上下文
     * @return 用户应用列表
     */
    public static List<AppListBean> getUserAppList(Context context) {
        List<AppListBean> appListBeans = new ArrayList<>();
        PackageManager packageManager = context.getPackageManager();
        List<ApplicationInfo> installedApplications = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
        
        for (ApplicationInfo applicationInfo : installedApplications) {
            // 过滤掉系统应用，只保留用户安装的应用
            if ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                try {
                    PackageInfo packageInfo = packageManager.getPackageInfo(applicationInfo.packageName, 0);
                    // 获取app名称
                    String name = packageInfo.applicationInfo.loadLabel(packageManager).toString();
                    
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                        appListBeans.add(new AppListBean(name,
                                // 判断是否系统应用
                                String.valueOf((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 ? 1 : 0), 
                                packageInfo.versionName,
                                String.valueOf(packageInfo.firstInstallTime),
                                (int) (new Date().getTime() / 1000), 
                                packageInfo.packageName,
                                String.valueOf(packageInfo.lastUpdateTime), 
                                String.valueOf(packageInfo.getLongVersionCode())));
                    } else {
                        appListBeans.add(new AppListBean(name,
                                // 判断是否系统应用
                                String.valueOf((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0 ? 1 : 0), 
                                packageInfo.versionName,
                                String.valueOf(packageInfo.firstInstallTime),
                                (int) (new Date().getTime() / 1000), 
                                packageInfo.packageName,
                                String.valueOf(packageInfo.lastUpdateTime), 
                                String.valueOf(packageInfo.versionCode)));
                    }
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        
        return appListBeans;
    }
}